package com.ymt.bpm.engine.listener.activity;

import com.ymt.bpm.appboot.SpringContext;
import com.ymt.bpm.engine.handler.activity.AsyncServiceTaskDelegate;
import com.ymt.bpm.engine.listener.engine.exception.EngineException;
import com.ymt.bpm.engine.listener.engine.exception.ErrorCode;
import com.ymt.bpm.service.platform.log.SysLogService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;

/**
 * Created by Johnny on 2016/12/24.
 */
public class AsyncServiceTaskDelegateListener implements JavaDelegate {

    private AsyncServiceTaskDelegate stDelegate;

    @Override
    public void execute(DelegateExecution delegateExecution) throws Exception {
        try {
            stDelegate = SpringContext.getBean(AsyncServiceTaskDelegate.class);
            stDelegate.execute(delegateExecution);
        } catch (Exception e) {
            e.printStackTrace();
            EngineException ee = new EngineException(ErrorCode.PI_FAIL,
                    "AsyncServiceTaskDelegateListener failed on event:" + delegateExecution.getEventName()+", executionId="+delegateExecution.getId(),
                    e);
            try {
                SysLogService logService = SpringContext.getBean(SysLogService.class);
                logService.expLog(delegateExecution.getTenantId(),
                        delegateExecution.getProcessInstanceId(),
                        delegateExecution.getCurrentActivityId(),
                        "",
                        "0",
                        null, null, e);
            } catch (Exception e1) {
                e.printStackTrace();
            }
            throw new RuntimeException(
                    ee);//for rollback
        }
    }

}
